package cn.lyx.xiaohoshu.auth.controller;

import cn.dev33.satoken.stp.StpUtil;
import cn.lyx.framework.biz.operationlog.aspect.ApiOperationLog;
import cn.lyx.framework.common.response.Response;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.concurrent.TimeUnit;

/**
 * @Author by lyx
 */
@RestController
@RequestMapping("/")
@Configuration
@RefreshScope
public class MainController {
    // http://localhost:8080/
    @RequestMapping("/")
    @ApiOperationLog(description = "测试页面")
    public Response<String> index() {
        return new Response<String>().success("Hello World");
    }

    @RequestMapping("/test")
    @ApiOperationLog(description = "测试LocalDateTime")
    public Response<?> user(@RequestHeader("userId") Long userId) {
        // User user = User.builder().createTime(LocalDateTime.now()).modifyTime(LocalDateTime.now()).build();
        return Response.success(userId);
    }

    @PostMapping("/test2")
    @ApiOperationLog(description = "测试接口2")
    public Response<User> test2(@RequestBody @Validated User user ) {
        return Response.success(user);
    }

    @PostMapping("/test3")
    @ApiOperationLog(description = "测试接口2")
    public Response<User> test3(@RequestBody @Validated User user) {
        int i = 1 / 0;
        return Response.success();
    }

    // 测试登录，浏览器访问： http://localhost:8080/user/doLogin?username=zhang&password=123456
    @RequestMapping("/user/doLogin")
    public String doLogin(String username, String password) {
        // 此处仅作模拟示例，真实项目需要从数据库中查询数据进行比对
        if("zhang".equals(username) && "123456".equals(password)) {
            StpUtil.login(10001);
            return "登录成功";
        }
        return "登录失败";
    }

    // 查询登录状态，浏览器访问： http://localhost:8080/user/isLogin
    @RequestMapping("/user/isLogin")
    public String isLogin() {
        return "当前会话是否登录：" + StpUtil.isLogin();
    }

    @RequestMapping("/user/logout")
    public String logout() {
        StpUtil.logout();
        return "logout";
    }

    @GetMapping("/alarm")
    public String sendAlarm() {
        return this.alarmType;
    }

    @Value("${alarm.type}")
    private String alarmType;

}
